package image;

import common.ConstVars;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * WRITE A BETTER DESCRIPTION!
 *
 * @author Mike (mcchristensen@email.wm.edu)
 */
public class ImageToTextConverter {

    private static FileWriter out;

    protected static void toFile(double[] vector, String name)
    {
        try
        {
            out.write(name);
            for(int i = 0;i < vector.length;i++)
                out.write(" " + vector[i]);
            out.write("\n");
        }
        catch(IOException e)
        {
            System.out.println("DATA NOT SAVED:");
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        if(args.length < 2) {
            System.err.println("args: inputDir outputFile [testingDataFlag]");
            System.exit(1);
        }
        boolean testingDataFlag = args.length >= 3;
        String fileNameTemp = "";
        try {
            out = new FileWriter(args[1], false);
            if (args[0].charAt(args[0].length()-1) != '/')
                args[0] += "/";
            File dir = new File(args[0]);

            String[] children = dir.list();
            if(children.length != 5) {
                System.err.println("Improperly formatted directory " + args + ". " + children.length + " children");
                System.exit(1);
            }
            out.write(ConstVars.IMG_DIM_2 + "\n");
            for(int i = 0; i < children.length; i++) {
                File cur = new File(args[0] + children[i]);

                String[] files = cur.list();
                for(int j = 0; j < files.length; j++) {
                    fileNameTemp = args[0] + children[i] + "/" + files[j];
                    BufferedImage temp = ImageFixer.getImageFromFile(args[0] + children[i] + "/" + files[j]);
                    if(testingDataFlag)
                    {
                      BufferedImage temp2 = ImageFixer.cropToFace(temp, 0, 0);
                      temp2 = ImageFixer.resizeImage(temp2, ConstVars.IMG_DIM, ConstVars.IMG_DIM);
                      double[] vector =  new double[ConstVars.IMG_DIM_2];
                      ImageFixer.imageToVector(temp2, vector);
                      toFile(vector, children[i]);
                    }
                    else
                    {
                      for(int xOffset = -6;xOffset <= 6;xOffset += 3) {
                        for(int yOffset = -6;yOffset <= 6;yOffset += 3) {
                          BufferedImage temp2 = ImageFixer.cropToFace(temp, xOffset, yOffset);
                          temp2 = ImageFixer.resizeImage(temp2, ConstVars.IMG_DIM, ConstVars.IMG_DIM);
                          double[] vector =  new double[ConstVars.IMG_DIM_2];
                          ImageFixer.imageToVector(temp2, vector);
                          toFile(vector, children[i]);
                        }
                      }
                    }
                }
            }
            out.close();
        } catch (Exception e) {
            System.out.println(args[0]);
            e.printStackTrace();
            System.out.println(fileNameTemp);
        }

    }
}
